package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cartItem
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody CartItem cartItem){
        this.cartService.addCart(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询当前用户的所有的购物车信息
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> listMongoCart(){
        return ResponseEntity.ok(this.cartService.listMongoCart());
    }

    @DeleteMapping("/{skuId}")
    public ResponseEntity<Void> deleteBySkuId(@PathVariable("skuId")Long skuId){

        this.cartService.deleteBySkuId(skuId);
        return ResponseEntity.ok().build();
    }
}
